/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doanlythuyet_javaoutlook.MyUserControl;

import BUS.MyContactBUS;
import DTO.MyContactDTO;
import doanlythuyet_javaoutlook.EventListener_ClickChuotVaoCayDuyetFile;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;


/**
 *
 * @author Administrator
 */
public class jTable_HienThiContact extends JTable {
    
    public jTable_HienThiContact(){
        //File file = moDialogChonAnh();
        setAutoCreateRowSorter(true);
    }

    //<editor-fold defaultstate="collapsed" desc="Dung de phat sinh su kien">
    //Các hàm sau phục vụ cho việc gởi sự kiện click chuột vào bảng ra ngoài (tham khảo từ nhiều nguồn trên mạng)
    //http://www.exampledepot.com/egs/java.util/CustEvent.html
    // Tạo một listener list
    protected javax.swing.event.EventListenerList listenerList =
            new javax.swing.event.EventListenerList();

    /**
     * Phát sinh sử kiện click chuột vào tree
     * @param evt tham số cho sự kiện click chuột vào tree (ở đây là tên của file đang được chọn)
     */
    // This private class is used to fire MyEvents
    void initEvent_ClickChuotVaoCayDuyetFile(String evt) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == EventListener_ClickChuotVaoCayDuyetFile.class) {
                ((EventListener_ClickChuotVaoCayDuyetFile) listeners[i + 1]).Event_ClickChuotVaoCayDuyetFile_Occurred(evt);
            }
        }
    }

    /**
     * Đăng ký sự kiện cho classes
     * @param listener  Sự kiện cần đăng ký
     */
    public void addEventListener_ClickChuotVaoCayDuyetFile(EventListener_ClickChuotVaoCayDuyetFile listener) {
        listenerList.add(EventListener_ClickChuotVaoCayDuyetFile.class, listener);
    }

    /**
     * Gỡ bỏ sự kiện khỏi classes
     * @param listener  Sự kiện cần gỡ bỏ
     */
    public void delEventListener_ClickChuotVaoCayDuyetFile(EventListener_ClickChuotVaoCayDuyetFile listener) {
        listenerList.remove(EventListener_ClickChuotVaoCayDuyetFile.class, listener);
    }
    //</editor-fold>
    public void DuaDanhSachMailVaoBang(ArrayList<MyContactDTO>DanhSachMail) throws IOException, SQLException{
            final String cacCotDuLieu[] = {"Tất cả","Họ tên","Nickname","Email","Địa chỉ", "Cơ quan", "Điện thoại", "ID"};
            Object DuLieu[][] = new Object[DanhSachMail.size()][cacCotDuLieu.length];

            for(int i = 0; i < DanhSachMail.size();i++){
                int index = 0;
                DuLieu[i][index++] = false;
               //for(int j = 1; i < model.) dulieu[index] = new Object();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Ten();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_NickName();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Email();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_DiaChi();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_CongTy();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_DienThoai();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Id();

            }
            final Object bangDuLieu[][] = DuLieu;
            DefaultTableModel modelBangHienThi = new DefaultTableModel(bangDuLieu,cacCotDuLieu) {
            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex){
                    case 0: return Boolean.class;
                    default: return String.class;
                }
            }
                @Override
                public int getColumnCount() {
                    return cacCotDuLieu.length;
                }
                @Override
                public String getColumnName(int column) {
                    return cacCotDuLieu[column];
                }
                @Override
                public int getRowCount() {
                    return bangDuLieu.length;
                }
                @Override
                public Object getValueAt(int row, int column) {
                    return bangDuLieu[row][column];
                }
                @Override
                public void setValueAt(Object value, int row, int column) {
                    bangDuLieu[row][column] = value;
                }
                @Override
                public boolean  isCellEditable(int row, int column) {
                    return column == 0;
                }
            };
addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                if (getSelectedRow() != -1){
                    initEvent_ClickChuotVaoCayDuyetFile(getValueAt(getSelectedRow(), getColumnCount() - 1).toString());
                }
            }

            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
            this.setModel(modelBangHienThi);

            //jTable_danhsachmail = new TableSelectionTest(cacCotDuLieu, bangDuLieu);
            TableColumn tc = this.getColumnModel().getColumn(0);
            tc.setCellEditor(this.getDefaultEditor(Boolean.class));
            tc.setCellRenderer(this.getDefaultRenderer(Boolean.class));
            tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));

            // <editor-fold defaultstate="collapsed" desc="Cai dat giao dien cho bang">
            //jTable_danhsachmail.setSize(500, 100);
            this.getColumnModel().getColumn(0).setMaxWidth(25);
            this.getColumnModel().getColumn(this.getColumnCount() - 1).setMinWidth(0);
            this.getColumnModel().getColumn(this.getColumnCount() - 1).setMaxWidth(0);
            this.getColumnModel().getColumn(this.getColumnCount() - 1).setPreferredWidth(0);
            //jTable_danhsachmail.
            // </ediort-fold>
    }

    private void DoiTrangThaiCotCheckBox (boolean isDuocChon){
        TableModel tableModel = this.getModel();
        for (int i = 0; i < tableModel.getRowCount(); i++){
            tableModel.setValueAt(isDuocChon, i, 0);
        }
    }

    public void refreshBang() {
        //thanh:refresh giao dien de? cap nhat
        try {
            MyContactBUS attachBUS = new MyContactBUS();
            ArrayList<MyContactDTO> cacContactDTO = attachBUS.layTatCaContact();
            DuaDanhSachMailVaoBang(cacContactDTO);
        } catch (SQLException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    class MyItemListener implements ItemListener{
     public void itemStateChanged(ItemEvent e) {
       Object source = e.getSource();
       if (source instanceof AbstractButton == false) return;
       boolean checked = e.getStateChange() == ItemEvent.SELECTED;
       DoiTrangThaiCotCheckBox(checked);
     }
   }
}
